<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- saved from url=(0028)http://jdbi.org/archive.html -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>


<title>abacus-common</title>

<script type="text/javascript" src="./css/ga.js"></script>
<script type="text/javascript">
	if (top.location != self.location)
		top.location.replace(self.location);
</script>

<link href="./css/css" rel="stylesheet" type="text/css"></link>
<link href="./css/css(1)" rel="stylesheet" type="text/css"></link>
<link rel="stylesheet" href="./css/syntax.css" type="text/css"></link>
<link rel="stylesheet" href="./css/screen.css" type="text/css"></link>

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-65247258-1', 'auto');
  ga('send', 'pageview');
</script>

</head>
<body>
	<div id="site">
		<h1>
			<a href="./index.html">abacus-common</a> <span id="navigation"> <a href="./docs.html">Docs</a> | <a href="./download.html">Download</a>
				| <a href="./api-docs/index.html">Javadoc</a> | <a href="./faq.html">FAQ</a> | <a href="./community.html">Community</a>
			</span>
		</h1>
	</div>

	<div id="content">

		<div id="page">
		
			<h1>Http Client</h1>
			<p>By the methods provided in <a href="./HttpClient_view.html">HttpClient</a>, it's super easy to send(get/post/put/delete...) String/Map/Entity/InputStream/Reader... request with JSON/XML... format. Here is a sample: </p>
<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #408080; font-style: italic">/*</span>
<span style="color: #408080; font-style: italic">To run the test, you need to configure servlet: EchoServlet in web.xml under tomcat and update the url accordingly.</span>
<span style="color: #408080; font-style: italic">The EchoServlet just sends back what the server receives from client.</span>

<span style="color: #408080; font-style: italic">&lt;servlet&gt;</span>
<span style="color: #408080; font-style: italic">     &lt;servlet-name&gt;echo&lt;/servlet-name&gt;</span>
<span style="color: #408080; font-style: italic">     &lt;servlet-class&gt;com.landawn.abacus.http.EchoServlet&lt;/servlet-class&gt;</span>
<span style="color: #408080; font-style: italic">&lt;/servlet&gt;        </span>
<span style="color: #408080; font-style: italic">...</span>
<span style="color: #408080; font-style: italic">&lt;servlet-mapping&gt;</span>
<span style="color: #408080; font-style: italic">     &lt;servlet-name&gt;echo&lt;/servlet-name&gt;</span>
<span style="color: #408080; font-style: italic">     &lt;url-pattern&gt;/echo&lt;/url-pattern&gt;</span>
<span style="color: #408080; font-style: italic">&lt;/servlet-mapping&gt;</span>
<span style="color: #408080; font-style: italic">*/</span>
<span style="color: #AA22FF">@Test</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">test_HttpClient</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
    <span style="color: #008000; font-weight: bold">final</span> String url <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;http://localhost:8080/abacus/echo&quot;</span><span style="color: #666666">;</span>
    <span style="color: #008000; font-weight: bold">final</span> HttpClient httpClient <span style="color: #666666">=</span> HttpClient<span style="color: #666666">.</span><span style="color: #7D9029">create</span><span style="color: #666666">(</span>url<span style="color: #666666">);</span>

    Account account <span style="color: #666666">=</span> createAccount<span style="color: #666666">();</span>
    String request <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;&lt;get&gt;&lt;entityId entityName=\&quot;Account\&quot;&gt;&lt;id&gt;&quot;</span> <span style="color: #666666">+</span> account<span style="color: #666666">.</span><span style="color: #7D9029">getId</span><span style="color: #666666">()</span> <span style="color: #666666">+</span> <span style="color: #BA2121">&quot;&lt;/id&gt;&lt;/entityId&gt;&lt;/get&gt;&quot;</span><span style="color: #666666">;</span>

    String response <span style="color: #666666">=</span> httpClient<span style="color: #666666">.</span><span style="color: #7D9029">get</span><span style="color: #666666">(</span>N<span style="color: #666666">.</span><span style="color: #7D9029">asMap</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;call&quot;</span><span style="color: #666666">,</span> request<span style="color: #666666">));</span>
    N<span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">(</span>response<span style="color: #666666">);</span>
    <span style="color: #408080; font-style: italic">// Response: call=&lt;get&gt;&lt;entityId entityName=&quot;Account&quot;&gt;&lt;id&gt;0&lt;/id&gt;&lt;/entityId&gt;&lt;/get&gt;</span>

    response <span style="color: #666666">=</span> httpClient<span style="color: #666666">.</span><span style="color: #7D9029">get</span><span style="color: #666666">(</span>account<span style="color: #666666">);</span>
    N<span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">(</span>response<span style="color: #666666">);</span>
    <span style="color: #408080; font-style: italic">// Response: id=0&amp;gui=c3f39f3b-7ebb-4575-a987-0b3a7ecb5ab5&amp;firstName=Jack&amp;lastName=Ye&amp;status=0&amp;lastUpdateTime=2015-08-04T00:24:45.411Z&amp;createTime=2015-08-04T00:24:45.411Z</span>

    response <span style="color: #666666">=</span> httpClient<span style="color: #666666">.</span><span style="color: #7D9029">post</span><span style="color: #666666">(</span>request<span style="color: #666666">);</span>
    N<span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">(</span>response<span style="color: #666666">);</span>
    <span style="color: #408080; font-style: italic">// Response: &lt;get&gt;&lt;entityId entityName=&quot;Account&quot;&gt;&lt;id&gt;0&lt;/id&gt;&lt;/entityId&gt;&lt;/get&gt;</span>
    assertEquals<span style="color: #666666">(</span>request<span style="color: #666666">,</span> response<span style="color: #666666">);</span>

    <span style="color: #408080; font-style: italic">// send entity object with JSON format</span>
    Map<span style="color: #666666">&lt;</span>String<span style="color: #666666">,</span> Object<span style="color: #666666">&gt;</span> settings <span style="color: #666666">=</span> N<span style="color: #666666">.</span><span style="color: #7D9029">asMap</span><span style="color: #666666">(</span>HTTP<span style="color: #666666">.</span><span style="color: #7D9029">CONTENT_FORMAT</span><span style="color: #666666">,</span> ContentFormat<span style="color: #666666">.</span><span style="color: #7D9029">JSON</span><span style="color: #666666">);</span>
    response <span style="color: #666666">=</span> httpClient<span style="color: #666666">.</span><span style="color: #7D9029">post</span><span style="color: #666666">(</span>account<span style="color: #666666">,</span> settings<span style="color: #666666">);</span>
    N<span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">(</span>response<span style="color: #666666">);</span>
    <span style="color: #408080; font-style: italic">// Response: {&quot;id&quot;:0, &quot;gui&quot;:&quot;8528c81f-6e98-4cea-aeeb-e60dcfadc9bc&quot;, &quot;firstName&quot;:&quot;Jack&quot;, &quot;lastName&quot;:&quot;Ye&quot;, &quot;status&quot;:0, &quot;lastUpdateTime&quot;:1438647775174, &quot;createTime&quot;:1438647775174}</span>
    assertEquals<span style="color: #666666">(</span>N<span style="color: #666666">.</span><span style="color: #7D9029">toJSON</span><span style="color: #666666">(</span>account<span style="color: #666666">),</span> response<span style="color: #666666">);</span>

    <span style="color: #408080; font-style: italic">// Specific response object is composed by JSON response from server.</span>
    Account account2 <span style="color: #666666">=</span> httpClient<span style="color: #666666">.</span><span style="color: #7D9029">post</span><span style="color: #666666">(</span>Account<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">,</span> account<span style="color: #666666">,</span> settings<span style="color: #666666">);</span>
    assertEquals<span style="color: #666666">(</span>account<span style="color: #666666">,</span> account2<span style="color: #666666">);</span>

    <span style="color: #408080; font-style: italic">// send entity object with JSON format and compressed by LZ4</span>
    settings <span style="color: #666666">=</span> N<span style="color: #666666">.</span><span style="color: #7D9029">asMap</span><span style="color: #666666">(</span>HTTP<span style="color: #666666">.</span><span style="color: #7D9029">CONTENT_FORMAT</span><span style="color: #666666">,</span> ContentFormat<span style="color: #666666">.</span><span style="color: #7D9029">JSON_LZ4</span><span style="color: #666666">);</span>
    response <span style="color: #666666">=</span> httpClient<span style="color: #666666">.</span><span style="color: #7D9029">post</span><span style="color: #666666">(</span>account<span style="color: #666666">,</span> settings<span style="color: #666666">);</span>
    N<span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">(</span>response<span style="color: #666666">);</span>
    <span style="color: #408080; font-style: italic">// Response: {&quot;id&quot;:0, &quot;gui&quot;:&quot;8528c81f-6e98-4cea-aeeb-e60dcfadc9bc&quot;, &quot;firstName&quot;:&quot;Jack&quot;, &quot;lastName&quot;:&quot;Ye&quot;, &quot;status&quot;:0, &quot;lastUpdateTime&quot;:1438647775174, &quot;createTime&quot;:1438647775174}</span>
    assertEquals<span style="color: #666666">(</span>N<span style="color: #666666">.</span><span style="color: #7D9029">toJSON</span><span style="color: #666666">(</span>account<span style="color: #666666">),</span> response<span style="color: #666666">);</span>

    <span style="color: #408080; font-style: italic">// send entity object with XML format</span>
    settings <span style="color: #666666">=</span> N<span style="color: #666666">.</span><span style="color: #7D9029">asMap</span><span style="color: #666666">(</span>HTTP<span style="color: #666666">.</span><span style="color: #7D9029">CONTENT_FORMAT</span><span style="color: #666666">,</span> ContentFormat<span style="color: #666666">.</span><span style="color: #7D9029">XML</span><span style="color: #666666">);</span>
    response <span style="color: #666666">=</span> httpClient<span style="color: #666666">.</span><span style="color: #7D9029">post</span><span style="color: #666666">(</span>account<span style="color: #666666">,</span> settings<span style="color: #666666">);</span>
    N<span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">(</span>response<span style="color: #666666">);</span>
    <span style="color: #408080; font-style: italic">// Response: &lt;account&gt;&lt;id&gt;0&lt;/id&gt;&lt;gui&gt;8528c81f-6e98-4cea-aeeb-e60dcfadc9bc&lt;/gui&gt;&lt;firstName&gt;Jack&lt;/firstName&gt;&lt;lastName&gt;Ye&lt;/lastName&gt;&lt;status&gt;0&lt;/status&gt;&lt;lastUpdateTime&gt;1438647775174&lt;/lastUpdateTime&gt;&lt;createTime&gt;1438647775174&lt;/createTime&gt;&lt;/account&gt;</span>
    assertEquals<span style="color: #666666">(</span>N<span style="color: #666666">.</span><span style="color: #7D9029">toXML</span><span style="color: #666666">(</span>account<span style="color: #666666">),</span> response<span style="color: #666666">);</span>

    <span style="color: #408080; font-style: italic">// send entity object with XML format and compressed by Snappy</span>
    settings <span style="color: #666666">=</span> N<span style="color: #666666">.</span><span style="color: #7D9029">asMap</span><span style="color: #666666">(</span>HTTP<span style="color: #666666">.</span><span style="color: #7D9029">CONTENT_FORMAT</span><span style="color: #666666">,</span> ContentFormat<span style="color: #666666">.</span><span style="color: #7D9029">XML_SNAPPY</span><span style="color: #666666">);</span>
    response <span style="color: #666666">=</span> httpClient<span style="color: #666666">.</span><span style="color: #7D9029">post</span><span style="color: #666666">(</span>account<span style="color: #666666">,</span> settings<span style="color: #666666">);</span>
    N<span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">(</span>response<span style="color: #666666">);</span>
    <span style="color: #408080; font-style: italic">// Response: &lt;account&gt;&lt;id&gt;0&lt;/id&gt;&lt;gui&gt;8528c81f-6e98-4cea-aeeb-e60dcfadc9bc&lt;/gui&gt;&lt;firstName&gt;Jack&lt;/firstName&gt;&lt;lastName&gt;Ye&lt;/lastName&gt;&lt;status&gt;0&lt;/status&gt;&lt;lastUpdateTime&gt;1438647775174&lt;/lastUpdateTime&gt;&lt;createTime&gt;1438647775174&lt;/createTime&gt;&lt;/account&gt;</span>
    assertEquals<span style="color: #666666">(</span>N<span style="color: #666666">.</span><span style="color: #7D9029">toXML</span><span style="color: #666666">(</span>account<span style="color: #666666">),</span> response<span style="color: #666666">);</span>
<span style="color: #666666">}</span>
</pre></div>

			

		</div>

	</div>
</body>

</html>